/**
 * 层级关系
 * @type {Object}
 */
var ZORDER = {
    BOARD: 10,
    WINDOW: 20,
    DIALOG: 100,
    LOADING: 150,
};

var BoardTag = 1024;

var lastViewWindow = null;


/**
 * 显示主模块的board层界面(每次打开一个新的board)
 * @param {String} boardName borad名字
 */
var showBoard = function (boardName) {

    if (boardName == 'HallBoard' && gameData.roomClubId && ClubDataManager.checkClubListHaveClubId(gameData.roomClubId)) {
        boardName = 'ClubBoard';
    }else if(!ClubDataManager.checkClubListHaveClubId(gameData.roomClubId)){
        gameData.roomClubId = null;
    }
    if (boardName == "RoomBoard" && window.networkEnd_createRoom) {
        window.show_createRoom = new Date().valueOf() - window.networkEnd_createRoom;
    }


    var originalName = boardName;
    if (window[boardName + '_' + app.classExtendSuffix]) {
        boardName += '_' + app.classExtendSuffix;
    }
    if (!window[boardName]) {
        cc.error('想要显示board : ' + boardName + '层界面不存在');
        return;
    }
    if (!window.curScene || !cc.sys.isObjectValid(window.curScene)) {
        window.curScene = new cc.Scene();
        cc.director.runScene(window.curScene);
    }
    var arg = arguments;
    var board = window.curScene.getChildByTag(BoardTag);
    if (board && cc.sys.isObjectValid(board)) {
        var childes = window.curScene.getChildren();
        for (var i = childes.length - 1; i >= 0; i--) {
            if (childes[i].viewTag == ViewTag.Window || (childes[i] instanceof Window)) {
                if (childes[i].isValid()) {
                    childes[i].removeFromParent(true);
                }
            }
        }
        board.removeFromParent(true);
    }
    if (window.hasOwnProperty('Timer')) {
        window.Timer.start('Board');
    }

    board = new window[boardName]();
    if (!(board instanceof Board)) {
        cc.error('想要显示的不是一个Board!!!');
        return;
    }
    board.setName(originalName);
    board.setTag(BoardTag);
    board.initView.apply(board, _.drop(arg));
    window.curScene.addChild(board);
    return board;
};


//
// /**
//  * 显示主模块的board层界面(每次打开一个新的board)
//  * @param {String} boardName borad名字
//  */
// var showBoard = function (boardName) {
//     var originalName = boardName;
//     if (window[boardName + '_' + app.classExtendSuffix]) {
//         boardName += '_' + app.classExtendSuffix;
//     }
//     if (!window[boardName]) {
//         cc.error('想要显示board : ' + boardName + '层界面不存在');
//         return;
//     }
//     window.sceneStack = window.sceneStack || [];
//     var newScene = new cc.Scene();
//     var arg = arguments;
//
//     window.curScene = newScene;
//     var board = new window[boardName]();
//     if (!(board instanceof Board)) {
//         cc.error('想要显示的不是一个Board!!!');
//         return;
//     }
//     board.setName(originalName);
//     board.initView.apply(board, _.drop(arg));
//     newScene.addChild(board, ZORDER.BOARD);
//     newScene.setName(originalName);
//     cc.director.runScene(new cc.TransitionFade(0.01, newScene));
//
//
//     if(!window.GameScene || !cc.sys.isObjectValid(window.GameScene)){
//         window.GameScene = new cc.Scene();
//         cc.director.runScene(window.GameScene);
//     }
//
// };


/**
 * 显示主模块的视窗界面
 * @param {String} windowName 视窗的名字
 */
var showWindow = function (windowName) {
    // if (windowName == 'WanFaWindow') {
    //     showLoading('正在解析房间配置...');
    //     var clubId = arguments[1];
    //     var currentWnafaIdex = arguments[2];
    //     setTimeout(function () {
    //         var createRoomWindow = null;
    //         if (clubId) {
    //             YYReporterUtil.add(ReporterType.Hall_ClickWanFa, 1);
    //             createRoomWindow = showWindow('CreateRoomWindow', 'hall', false, clubId, (currentWnafaIdex < 5), currentWnafaIdex);
    //         } else {
    //             YYReporterUtil.add(ReporterType.Hall_ClickCreateRoom, 1);
    //             createRoomWindow = showWindow('CreateRoomWindow', 'hall');
    //         }
    //         createRoomWindow._rootNode.x = cc.winSize.width / 2;
    //         createRoomWindow._rootNode.y = cc.winSize.height / 2;
    //     }, 1);
    //
    //     // if (clubId) {
    //     //     showWindow('CreateRoomWindow', 'hall', false, clubId, (currentWnafaIdex < 5), currentWnafaIdex);
    //     // } else {
    //     //     showWindow('CreateRoomWindow', 'hall');
    //     // }
    //     return;
    // }
    var originalName = windowName;
    if (window[windowName + '_' + app.classExtendSuffix]) {
        windowName += '_' + app.classExtendSuffix;
    }
    if (!window[windowName]) {
        cc.error('想要显示的视窗 : ' + windowName + '不存在');
        return;
    }
    if (!window.curScene) {
        cc.error('window显示到的场景为空');
        return;
    }
    if (!cc.sys.isObjectValid(window.curScene)) {
        return;
    }
    var win = new window[windowName]();
    if (!(win instanceof Window)) {
        cc.error('想要显示的不是一个视窗 :' + windowName);
        return;
    }

    var arg = arguments;
    win.initView.apply(win, _.drop(arg));
    win.viewTag = ViewTag.Window;
    win.setName(originalName);
    //如果这个字段是true  在showWindow的时候 会保存这个window的name，
    //断线重现后会检测如果有保存的name自动打开对应的window
    if (win.recordWindowName) {
        lastViewWindow = {};
        lastViewWindow.windowName = windowName;
        lastViewWindow.value = _.drop(arg);
    }
    // cc.log('显示window:' + windowName);
    window.curScene.addChild(win, ZORDER.WINDOW);
    // window.curScene.addChild(win);
    return win;
};

/**
 * 显示老的窗口
 * @param {String} windowName 视窗的名字
 */
var showOldWindow = function (windowName) {
    var originalName = windowName;
    if (window[windowName + '_' + app.classExtendSuffix]) {
        windowName += '_' + app.classExtendSuffix;
    }
    if (!window[windowName]) {
        cc.error('想要显示的视窗 : ' + windowName + '不存在');
        return;
    }
    if (!window.curScene) {
        cc.error('window显示到的场景为空');
        return;
    }
    if (!cc.sys.isObjectValid(window.curScene)) {
        return;
    }
    var win = new window[windowName]();
    win.viewTag = ViewTag.Window;
    win.initWindow.apply(win, _.drop(arguments));
    win.setName(originalName);
    window.curScene.addChild(win, ZORDER.WINDOW);
    // window.curScene.addChild(win);
    return win;
};


/**
 * 显示金币场视窗界面
 * @param {String} windowName 视窗的名字
 */
var showGoldWindow = function (windowName) {
    var originalName = windowName;
    if (window[windowName + '_' + app.classExtendSuffix]) {
        windowName += '_' + app.classExtendSuffix;
    }
    if (!window[windowName]) {
        cc.error('想要显示的视窗 : ' + windowName + '不存在');
        return;
    }
    var win = new window[windowName]();
    if (!(win instanceof Window)) {
        cc.error('想要显示的不是一个视窗 :' + windowName);
        return;
    }

    var arg = arguments;
    win.initView.apply(win, _.drop(arg));
    win.setName(originalName);
    //如果这个字段是true  在showWindow的时候 会保存这个window的name，
    //断线重现后会检测如果有保存的name自动打开对应的window
    if (win.recordWindowName) {
        lastViewWindow = {};
        lastViewWindow.windowName = windowName;
        lastViewWindow.value = _.drop(arg);
    }
    cc.director.getRunningScene().addChild(win, ZORDER.WINDOW);
    return win;
};

/**
 * 添加主模块的组件
 * @param {String} unitName 组件名
 * @param {cc.Node} parent 组件添加位置
 * @return {Unit}
 */
var addUnit = function (unitName, parent) {
    var originalName = unitName;
    if (window[unitName + '_' + app.classExtendSuffix]) {
        unitName += '_' + app.classExtendSuffix;
    }
    if (!window[unitName]) {
        cc.error('想要添加的组件 : ' + unitName + '不存在');
        return null;
    }
    var unit = new window[unitName]();
    if (!(unit instanceof Unit)) {
        cc.error('想要显示的不是一个组件 :' + unitName);
        return null;
    }
    var arg = arguments;
    unit.initView.apply(unit, _.drop(arg));
    unit.setName(originalName);
    parent.addChild(unit);
    return unit;
};

/**
 * 添加直接构造的组件
 * @param {String} itemName 组件名
 * @param {cc.Node} parent 组件添加位置
 * @return {Unit}
 */
var addItem = function (itemName, parent) {
    var originalName = itemName;
    if (window[itemName + '_' + app.classExtendSuffix]) {
        itemName += '_' + app.classExtendSuffix;
    }
    if (!window[itemName]) {
        cc.error('想要添加的组件 : ' + itemName + '不存在');
        return null;
    }
    var item = new window[itemName]();
    if (!(item instanceof Item)) {
        cc.error('想要显示的不是一个组件 :' + itemName);
        return null;
    }
    item.initView.apply(item, _.drop(arguments, 2));
    item.setName(originalName);
    parent.addChild(item);
    return item;
};


/**
 * 添加点击漂字
 * @param msg  内容
 */
var showToast = function (msg) {
    var toast = new cc.LayerColor();
    toast.width = msg.length * 40;
    toast.height = 40;
    toast.setOpacity(125);

    var txt = new ccui.Text();
    txt.setString(msg);
    txt.setFontSize(28);
    txt.setColor(cc.color(253, 255, 255));
    txt.setPosition(toast.width * 0.5, toast.height * 0.5);
    txt.setName('txt');
    toast.addChild(txt, 1);

    toast.setPosition(cc.p(display.cx - toast.width * 0.5, display.cy + 230));

    var moveTo = cc.moveBy(1.5, cc.p(0, 50));
    var delay = cc.delayTime(1.0);
    var fadeOut = cc.fadeOut(0.5);
    var seq = cc.sequence(delay, fadeOut);
    var act = cc.spawn(moveTo, seq);
    var clean = cc.removeSelf();
    var actSeq = cc.sequence(act, clean);
    toast.runAction(actSeq);
    cc.director.getRunningScene().addChild(toast, ZORDER.DIALOG);
};

/**
 * 显示主模块的对话框
 * @param {String} dialogName 对话框的名字
 */
var showDialog = function (dialogName) {
    var originalName = dialogName;
    if (window[dialogName + '_' + app.classExtendSuffix]) {
        dialogName += '_' + app.classExtendSuffix;
    }
    if (!window[dialogName]) {
        cc.error('想要显示的对话框 : ' + dialogName + '不存在');
        return;
    }
    if (!window.curScene) {
        cc.error('dialog显示到的场景为空');
        return;
    }
    if (!cc.sys.isObjectValid(window.curScene)) {
        return;
    }
    var dialog = new window[dialogName]();
    if (!(dialog instanceof Dialog)) {
        cc.error('想要显示的不是一个对话框 :' + dialogName);
        return;
    }
    var arg = arguments;
    dialog.initView.apply(dialog, _.drop(arg));
    dialog.setName(originalName);
    window.curScene.addChild(dialog, ZORDER.DIALOG);
    return dialog;
};

/**
 * 显示警告
 * @param {String} content 显示文本
 * @param {Function} [confirmCb] 确定回调
 */
var showAlert = function (content, confirmCb) {
    hideLoading();
    if (window.alertDialog && window.alertDialog.isValid()) {
        window.alertDialog.close();
        window.alertDialog = null;
    }
    window.alertDialog = showDialog('AlertDialog', content, confirmCb);
    if (window.alertDialog) {
        window.alertDialog.setLocalZOrder(ZORDER.DIALOG);
    }
};


/**
 * 显示对话框
 * @param {String} content 显示文本
 * @param {Function} [confirmCb] 确定回调
 * @param {Function} [cancelCb] 取消回调
 */
var showMessageBox = function (content, confirmCb, cancelCb) {
    return showDialog('NormalDialog', content, confirmCb, cancelCb);
};


/**
 * 显示加载
 */
var showLoading = function (content,type) {
    cc.log('showLoading');
    if (!window.curScene || !window.curScene.isValid()) {
        return ;
    }
    var loadingDialog = window.curScene.getChildByName('LoadingDialog');
    if (!loadingDialog) {
        loadingDialog = showDialog('LoadingDialog', content,type);
    }
    if (loadingDialog) {
        loadingDialog.setData(content,type);
        loadingDialog.show();
        loadingDialog.setLocalZOrder(ZORDER.LOADING);
    }
};

/**
 * 隐藏加载
 */
var hideLoading = function () {
    cc.log('hideLoading');
    if (!window.curScene || !window.curScene.isValid()) {
        return ;
    }
    var loadingDialog = window.curScene.getChildByName('LoadingDialog');
    if (loadingDialog) {
        loadingDialog.hide();
    }
};


/**
 * 显示房间
 */
var showRoom = function (data, roomDataType) {
    return showBoard('RoomBoard', data, roomDataType);
};

/***
 * 显示downLoading
 * @param content1
 * @param content2
 */
var showDownLoading = function (content1, content2) {
    if (!window.downloadingDialog || !window.downloadingDialog.isValid()) {
        window.downloadingDialog = showDialog('DownLoadingDialog', content1, content2);
    }
    if (!window.downloadingDialog) {
        hideLoading();
        return;
    }
    window.downloadingDialog.setContent(content1, content2);
    window.downloadingDialog.show();
    window.downloadingDialog.setLocalZOrder(ZORDER.LOADING);
    hideLoading();
};

/***
 * 隐藏downLoading
 */
var hideDownLoading = function () {
    if (window.downloadingDialog && window.downloadingDialog.isValid()) {
        window.downloadingDialog.hide();
    } else {
        window.downloadingDialog = null;
    }
};

/**
 * 显示断线界面
 * @param {string} board
 */
var showDisconnectDialog = function (board) {
    hideLoading();
    if (window.disconnectDialog && window.disconnectDialog.isValid()) {
        window.disconnectDialog.close();
    }
    window.disconnectDialog = showDialog('DisconnectDialog', board);
};

/**
 * 获取版本号
 * @param key 模板
 */
var getVersion = function (key) {
    var versiontxt = window.curVersion;
    if (key) {
        // var subArr = getLocalVersion();
        // var sub = '';
        // if (subArr) sub = subArr[key];
        // if(sub){
        //     versiontxt = window.curVersion + '-' + sub;
        // }
    }
    var content = window.curScene.getChildByName('Version');
    if (!content) {
        content = new ccui.Text();
        content.setFontSize(20);
        content.setAnchorPoint(cc.p(1, 0.5));
        content.setPosition(cc.p((window.DesignSize ? window.DesignSize.width : cc.winSize.width) - 50, 10));
        content.setColor(cc.color(255, 255, 255));
        content.enableOutline(cc.color(171, 171, 171), 1);
        content.setName('Version');
        window.curScene.addChild(content, 2000);
    }
    content.setString(versiontxt);
};

/**
 * 房间类型
 * @type {Object}
 */
var ROOM_DATA_TYPE = {
    CREATE: 0,
    RECONNECT: 1,
    REPLAY: 2
};

